//
//  GWChannelsCell.m
//  GiftWanted
//
//  Created by lanou on 15/11/10.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWChannelsCell.h"
#import "GWChannelsGroupModel.h"
#import "UIImageView+WebCache.h"
#import "GWChannelsModel.h"
#import "GWChannelCell.h"
#import "GWRaidersDetailViewController.h"
#import "GWChannelsDetailViewController.h"

#define x (SCREEN_WIDTH-50)/4

@interface GWChannelsCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation GWChannelsCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
        layout.itemSize = CGSizeMake(x, x+20);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 60+2*x) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[GWChannelCell class] forCellWithReuseIdentifier:@"flCell"];
        [self.contentView addSubview:_collectionView];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 40, 20)];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_titleLabel];
    }
    return self;
}

- (void)set {
    CGFloat height = 0;
    if (self.channelsArr.count > 8) {
        height = 80+5*x;
    } else {
        height = 80+2*x;
    }
    _collectionView.frame = CGRectMake(0, 20, SCREEN_WIDTH, height);
    _titleLabel.text = self.title;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.channelsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GWChannelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"flCell" forIndexPath:indexPath];
    GWChannelsModel *model = self.channelsArr[indexPath.item];
    [cell setConfigureModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GWChannelsDetailViewController *channelsVC = [[GWChannelsDetailViewController alloc] init];
    GWChannelsModel *model = _channelsArr[indexPath.item];
    channelsVC.name = model.name;
    channelsVC.id_s = model.id_s;
    [self.naviC pushViewController:channelsVC animated:YES];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
